package pe.com.hermes.hermetic.web.controller.general;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pe.com.hermes.hermetic.beans.ClienteBean;
import pe.com.hermes.hermetic.beans.ClienteCorpBean;
import pe.com.hermes.hermetic.negocio.service.ClienteService;
import pe.com.hermes.hermetic.negocio.service.HermeticFacade;
import pe.com.hermes.hermetic.negocio.ultil.ConstantesHermetic;
import pe.com.hermes.hermetic.negocio.ultil.Util;
import pe.com.hermes.hermetic.web.controller.base.IController;
import pe.com.hermes.hermetic.web.forms.HMF1001Form;
import pe.com.hermes.hermetic.web.forms.SelecClienteCorpForm;
import pe.com.hermes.hermetic.web.forms.SelecClienteForm;
import pe.com.hermes.hermetic.web.forms.SelecSucursalForm;

@Controller
@RequestMapping("/selecClienteCorp.html")
public class SeleccionarClienteCorpController implements IController{
    
  protected final static Logger logger = Logger.getLogger(SeleccionarPuntoController.class);
 // @Autowired
 // private HermeticFacade servicioHermetic; 
  @Autowired
  private ClienteService clienteService; 
  
  @RequestMapping(params={"accion=selecClienteC"})   
  protected String selecClienteC(ModelMap model,HttpServletRequest request)throws Exception{
      try {
          logger.info(this.getClass().toString()+" "+"metodo: GET");
          SelecClienteCorpForm form = new SelecClienteCorpForm();            
          int codCorp = (Integer)request.getSession(false).getAttribute("codCorp");       
          String usuWeb = (String)request.getSession(false).getAttribute("usuario");
          
          List listClientesCorp = clienteService.getListClieByCorp(codCorp,usuWeb,"");
                    
          String[] chksClieCorp = (String[])request.getSession(false).getAttribute("chksClieCorp");
          listClientesCorp = Util.setChksSession(chksClieCorp, listClientesCorp);                
          
          /*Si es que solo hay un check de Cliente corp en -1 (TODOS), quitarlo*/
          if (chksClieCorp!=null && chksClieCorp.length>0 && chksClieCorp[0]!=null && chksClieCorp[0].equals("-1")){
              request.getSession(false).setAttribute("chksClieCorp",null);
          }
          form.setListClientesCorp(listClientesCorp);
          request.getSession(false).setAttribute("listClientesCorp",listClientesCorp);
          model.put("listClientesCorp",listClientesCorp);     
          model.put(FORM_KEY,form);
          return FORM_SELECT_CLIENTE_CORP;
      }
      catch (Exception e) {
          logger.error(e);
          return FORM_ERROR_POPUP_VIEW;
      }
  }
  
  
  
  @RequestMapping(params={"accion=selectCodCliente","codCliente"}) 
  protected @ResponseBody String[] selectCodCliente(@RequestParam("codCliente") String codCliente,@ModelAttribute(FORM_KEY) SelecClienteCorpForm form,HttpServletRequest request)throws Exception{        
      logger.info(this.getClass().toString()+" "+"metodo: selectCodCliente");        
      String[] chksClieCorp = (String[])request.getSession(false).getAttribute("chksClieCorp");
      if(("").equals(codCliente))
          return chksClieCorp; 
      if(chksClieCorp!=null)
      {
          for(int i=0;i<chksClieCorp.length;i++){                
              if(chksClieCorp[i]!=null&&chksClieCorp[i].equals(codCliente)){
                  break;
              }
              if(chksClieCorp[i]==null){
                  chksClieCorp[i] = codCliente;
                  break;
              }
          }
      }
      else{
          logger.info("recien se crea con el codigo "+codCliente);
          chksClieCorp= new String[1000];
          chksClieCorp[0]=codCliente;
      }
      request.getSession(false).setAttribute("chksClieCorp",chksClieCorp);        
      return chksClieCorp;
  }
  
  @RequestMapping(params={"accion=deselectCodCliente","codCliente"}) 
  protected @ResponseBody String[] deselectCodCliente(@RequestParam("codCliente") String codCliente,@ModelAttribute(FORM_KEY) SelecClienteCorpForm form,HttpServletRequest request)throws Exception{        
      logger.info(this.getClass().toString()+" "+"metodo: deselectCodCliente");   
      
      String[] chksClieCorp = (String[])request.getSession(false).getAttribute("chksClieCorp");
      
      if(chksClieCorp!=null && chksClieCorp.length > 0)
          for(int i=0;i<chksClieCorp.length;i++){                
              if(chksClieCorp[i]!=null && chksClieCorp[i].equals(codCliente)){
                  chksClieCorp[i] = null;
                  break;
              }
          }
      
      request.getSession(false).setAttribute("chksClieCorp",chksClieCorp); 
      
      return chksClieCorp;
  }
  
  
  @RequestMapping(params={"accion=selectTodos","codTodos"}) 
  protected String selectTodos(@RequestParam("codTodos") boolean codTodos ,ModelMap model,HttpServletRequest request)throws Exception{
      logger.info(this.getClass().toString()+" "+"metodo: GET");
      //SelecClienteForm form = (SelecClienteForm)model.get("form");//new SelecCuentaForm();
       SelecClienteCorpForm form = new SelecClienteCorpForm();
      //Asigno el check a todos los elenmentos de la lista Cuenta        
       
      List listClientes = (List)request.getSession(false).getAttribute("listClientesCorp"); 
      
      listClientes = Util.setChksTodos(codTodos, listClientes);        
          //form.setListClientes(listClientes);
      
      //Subo a session los todos los Checks Cuenta
      if (codTodos) {
          String[] chksCliente = Util.listToStringArray(listClientes);
          request.getSession(false).setAttribute("chksClieCorp",chksCliente);       
      }else {
          request.getSession(false).setAttribute("chksClieCorp",null);        
      }
      
      request.getSession(false).setAttribute("listClientesCorp",listClientes);
      model.put("listClientesCorp",listClientes);
      model.put(FORM_KEY,form);
      return FORM_SELECT_CLIENTE_CORP;
  }
  
  @RequestMapping(params={"accion=buscarCliente","desClienteCorp"}) 
  protected String buscarCliente(@RequestParam("desClienteCorp") String desClienteCorp ,ModelMap model,HttpServletRequest request)throws Exception{
              logger.info(this.getClass().toString()+" "+"metodo: GET");
      SelecClienteCorpForm form = new SelecClienteCorpForm();
      int codCorp = (Integer)request.getSession(false).getAttribute("codCorp"); 
      String[] chksCliente = (String[])request.getSession(false).getAttribute("chksClieCorp");
      //List listSucursales = servicioHermetic.getSucursalesNoTodos();
      String usuWeb = (String)request.getSession(false).getAttribute("usuario");
      List listClientesCorp = clienteService.getListClieByCorp(codCorp, usuWeb, desClienteCorp);
      listClientesCorp = Util.setChksSession(chksCliente,listClientesCorp);
      //form.setListClientes(listClientes);
      //if(desClienteCorp.equals(""))
      request.getSession(false).setAttribute("listClientesCorp",listClientesCorp);
      model.put("listClientesCorp",listClientesCorp);
      model.put(FORM_KEY,form);
      return FORM_SELECT_CLIENTE_CORP;
  }
  
  @RequestMapping(params={"accion=searchDesCliente"}) 
  protected @ResponseBody String[] searchDesCliente(HttpServletRequest request)throws Exception{        
      logger.info(this.getClass().toString()+" "+"metodo: searchDesCliente");        
      List listModulos = (List)request.getSession(false).getAttribute("listClientesCorp");
      String[] arrayModulos = new String[listModulos.size()];
      for(int i=0;i<listModulos.size();i++){
          ClienteCorpBean clie = (ClienteCorpBean)listModulos.get(i);
          arrayModulos[i] = clie.getDesRazoSoci();
      }
      return arrayModulos;
  }
  
}
